package com.hxci.pointmap.dao;

import com.hxci.pointmap.model.User;
import com.hxci.pointmap.utils.DBUtils;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class LoginDAO {
    public List<Map<String, Object>> queryUserList(String sql, List paramList) {
        DBUtils dbUtils = new DBUtils();
        List<Map<String, Object>> resultList = dbUtils.doQuery(sql, paramList);
        return resultList;
    }

    /**
     * 查询所有用户
     */
    public List<User> getAllUsers() {
        String sql = "SELECT id, login_account, login_pwd, user_name, create_time, update_time FROM sys_user ORDER BY create_time DESC";
        DBUtils dbUtils = new DBUtils();
        List<Map<String, Object>> resultList = dbUtils.doQuery(sql, new ArrayList());
        List<User> userList = new ArrayList<>();

        for (Map<String, Object> map : resultList) {
            User user = new User();
            user.setId(Integer.parseInt(map.get("id").toString()));
            user.setLoginAccount(map.get("login_account").toString());
            user.setLoginPwd(map.get("login_pwd").toString());
            user.setUserName(map.get("user_name") != null ? map.get("user_name").toString() : "");
            user.setCreateTime((Timestamp) map.get("create_time"));
            user.setUpdateTime((Timestamp) map.get("update_time"));
            userList.add(user);
        }

        return userList;
    }

    /**
     * 根据ID查询用户
     */
    public User getUserById(int id) {
        String sql = "SELECT id, login_account, login_pwd, user_name, create_time, update_time FROM sys_user WHERE id = ?";
        List params = new ArrayList();
        params.add(id);

        DBUtils dbUtils = new DBUtils();
        List<Map<String, Object>> resultList = dbUtils.doQuery(sql, params);

        if (resultList.size() > 0) {
            Map<String, Object> map = resultList.get(0);
            User user = new User();
            user.setId(Integer.parseInt(map.get("id").toString()));
            user.setLoginAccount(map.get("login_account").toString());
            user.setLoginPwd(map.get("login_pwd").toString());
            user.setUserName(map.get("user_name") != null ? map.get("user_name").toString() : "");
            user.setCreateTime((Timestamp) map.get("create_time"));
            user.setUpdateTime((Timestamp) map.get("update_time"));
            return user;
        }

        return null;
    }

    /**
     * 根据登录账号查询用户
     */
    public User getUserByLoginAccount(String loginAccount) {
        String sql = "SELECT id, login_account, login_pwd, user_name, create_time, update_time FROM sys_user WHERE login_account = ?";
        List params = new ArrayList();
        params.add(loginAccount);

        DBUtils dbUtils = new DBUtils();
        List<Map<String, Object>> resultList = dbUtils.doQuery(sql, params);

        if (resultList.size() > 0) {
            Map<String, Object> map = resultList.get(0);
            User user = new User();
            user.setId(Integer.parseInt(map.get("id").toString()));
            user.setLoginAccount(map.get("login_account").toString());
            user.setLoginPwd(map.get("login_pwd").toString());
            user.setUserName(map.get("user_name") != null ? map.get("user_name").toString() : "");
            user.setCreateTime((Timestamp) map.get("create_time"));
            user.setUpdateTime((Timestamp) map.get("update_time"));
            return user;
        }

        return null;
    }

    /**
     * 添加用户
     */
    public int addUser(User user) {
        String sql = "INSERT INTO sys_user (login_account, login_pwd, user_name) VALUES (?, ?, ?)";
        List params = new ArrayList();
        params.add(user.getLoginAccount());
        params.add(user.getLoginPwd());
        params.add(user.getUserName());

        DBUtils dbUtils = new DBUtils();
        return dbUtils.doUpdate(sql, params);
    }

    /**
     * 更新用户
     */
    public int updateUser(User user) {
        String sql = "UPDATE sys_user SET login_account = ?, login_pwd = ?, user_name = ? WHERE id = ?";
        List params = new ArrayList();
        params.add(user.getLoginAccount());
        params.add(user.getLoginPwd());
        params.add(user.getUserName());
        params.add(user.getId());

        DBUtils dbUtils = new DBUtils();
        return dbUtils.doUpdate(sql, params);
    }

    /**
     * 删除用户
     */
    public int deleteUser(int id) {
        String sql = "DELETE FROM sys_user WHERE id = ?";
        List params = new ArrayList();
        params.add(id);

        DBUtils dbUtils = new DBUtils();
        return dbUtils.doUpdate(sql, params);
    }

    /**
     * 检查登录账号是否存在
     */
    public boolean checkLoginAccountExists(String loginAccount) {
        String sql = "SELECT COUNT(*) as count FROM sys_user WHERE login_account = ?";
        List params = new ArrayList();
        params.add(loginAccount);

        DBUtils dbUtils = new DBUtils();
        List<Map<String, Object>> resultList = dbUtils.doQuery(sql, params);

        if (resultList.size() > 0) {
            int count = Integer.parseInt(resultList.get(0).get("count").toString());
            return count > 0;
        }

        return false;
    }
}
